set(whitespaces_ "[\t\n\r ]*")

file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_rpm")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_rpm/BUILD")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_rpm/BUILDROOT")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_rpm/RPMS")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_rpm/SOURCES")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_rpm/SPECS")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/test_rpm/SRPMS")

# make sure that we are using the version of cmake and cpack that we are testing
get_filename_component(cpack_path_ "${CMAKE_CPACK_COMMAND}" DIRECTORY)
set(ENV{PATH} "${cpack_path_}:$ENV{PATH}")

execute_process(COMMAND ${RPMBUILD_EXECUTABLE} --define "_topdir ${CMAKE_CURRENT_BINARY_DIR}/test_rpm" --rebuild ${FOUND_FILE_1}
      RESULT_VARIABLE result_
      ERROR_VARIABLE  error_
      OUTPUT_QUIET
  )

string(REPLACE "\n" "\n rpmbuild-err> " rpmbuild_err "\n${error_}")
string(REPLACE "\n" "\n rpmbuild-res> " rpmbuild_res "\n${result_}")
string(CONCAT output_error_message_
  "${RPMBUILD_EXECUTABLE} error:${rpmbuild_err}\n"
  "${RPMBUILD_EXECUTABLE} result:${rpmbuild_res}\n"
  "${output_error_message}"
  )

# expected file content are test_prog and optional build-id links that are
# generated by rpmbuild (introduced in rpm 4.13.0.1)
set(EXPECTED_FILE_CONTENT_ "^/foo${whitespaces_}/foo/test_prog(${whitespaces_}.*\.build-id.*)*$")
set(EXPECTED_FILE_ "${CMAKE_CURRENT_BINARY_DIR}/test_rpm/RPMS/*.rpm")

file(GLOB_RECURSE FOUND_FILE_ RELATIVE "${CMAKE_CURRENT_BINARY_DIR}/test_rpm/RPMS" "${EXPECTED_FILE_}")
list(APPEND foundFiles_ "${FOUND_FILE_}")
list(LENGTH FOUND_FILE_ foundFilesCount_)

if(foundFilesCount_ EQUAL 1)
  unset(PACKAGE_CONTENT)
  getPackageContent("${CMAKE_CURRENT_BINARY_DIR}/test_rpm/RPMS/${FOUND_FILE_}" "PACKAGE_CONTENT")

  string(REGEX MATCH "${EXPECTED_FILE_CONTENT_}"
      expected_content_list "${PACKAGE_CONTENT}")

  if(NOT expected_content_list)
    string(REPLACE "\n" "\n actual> " msg_actual "\n${PACKAGE_CONTENT}")
    string(REPLACE "\n" "\n expect> " msg_expected "\n${EXPECTED_FILE_CONTENT_}")
    string(REPLACE "\r" "\\r" msg_expected "${msg_expected}")
    string(REPLACE "\t" "\\t" msg_expected "${msg_expected}")
    message(FATAL_ERROR
      "Unexpected file content!\n"
      "The content was:${msg_actual}\n"
      "which does not match:${msg_expected}\n"
      "${output_error_message_}")
  endif()
elseif(foundFilesCount_ EQUAL 0)
  message(FATAL_ERROR
    "Found no files!\n"
    "Globbing expression:\n '${EXPECTED_FILE_}'\n"
    "${output_error_message_}")
else()
  message(FATAL_ERROR
    "Found more than one file!\n"
    "Found files count '${foundFilesCount_}'.\n"
    "Files:\n '${FOUND_FILE_}'\n"
    "Globbing expression:\n '${EXPECTED_FILE_}'\n"
    "${output_error_message_}")
endif()

# check that there were no extra files generated
foreach(all_files_glob_ IN LISTS ALL_FILES_GLOB)
  file(GLOB foundAll_ RELATIVE "${CMAKE_CURRENT_BINARY_DIR}/test_rpm/RPMS" "${all_files_glob_}")
  list(APPEND allFoundFiles_ "${foundAll_}")
endforeach()

list(LENGTH foundFiles_ foundFilesCount_)
list(LENGTH allFoundFiles_ allFoundFilesCount_)

if(NOT foundFilesCount_ EQUAL allFoundFilesCount_)
  message(FATAL_ERROR
    "Found more files than expected!\n"
    "Found files:\n '${allFoundFiles_}'\n"
    "${output_error_message_}")
endif()
